package svgedit.gui.actions;

import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.swing.AbstractAction;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

import svgedit.gui.Frame;
import svgedit.gui.PNGFileFilter;
import svgedit.gui.SVGFileFilter;

@SuppressWarnings("serial")
public class SaveAsPNGAction extends AbstractAction{

	private Frame frame;
	int width = 100;
	int height = 100;
	/** Create this action for the given frame */
	public SaveAsPNGAction(Frame frame) {
		super("Export to PNG");
		this.frame = frame;
	}


	@Override
	public void actionPerformed(ActionEvent arg0) {
		File file = frame.getDocument().getFile();
		JFileChooser dialog;
		if (file != null)
			dialog = new JFileChooser(file);
		else
			dialog = new JFileChooser(frame.getPreferences().getDefaultPath());
		dialog.addChoosableFileFilter(new PNGFileFilter());
		dialog.showSaveDialog(null);

		file = dialog.getSelectedFile();
		if (file != null) {
			if (file.exists()) {
				if (JOptionPane.showConfirmDialog(frame.getRootPane(),
						String.format("Do you want to replace the existing file named %s?",
								file.getName()), "Overwrite existing file?",
								JOptionPane.OK_CANCEL_OPTION) == JOptionPane.CANCEL_OPTION)
					return;
			}
			frame.getPreferences().setDefaultPath(file.getPath());
			try {
				frame.savePNGFile(file);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
	}
}
